//
//  WXClient.m
//  BossZP
//
//  Created by Raymond~ on 14-4-16.
//  Copyright (c) 2014年 com.dlnu.*. All rights reserved.
//

#import "WXClient.h"

#import "SDWebImageManager+YS.h"
@implementation WXClient

+ (WXClient *)sharedClient{
    static WXClient * singleClient;
    static dispatch_once_t pred;
    dispatch_once(&pred,^{
        singleClient=[[self alloc] initSingleton];
    } );
    return singleClient;
}

- (id)initSingleton
{
    if(self=[super init]){
//        [WXApi registerApp:WXAPP_ID];
    }
    return self;
}

- (void) sendTextContent:(NSString *)textContent
                andScene:(int)scene{
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.text = textContent;
    req.bText = YES;
    req.scene = scene;
    [WXApi sendReq:req];
}

- (BOOL)handleOpenURL:(NSURL*)url{
    
    return [WXApi handleOpenURL:url delegate:self];
}
- (void)sendLinkContentWithURlStr:(NSString *)urlStr
                         andTitle:(NSString *)title
                   andDescription:(NSString *)description
                         andImage:(UIImage *)image
                         andScene:(int)scene {
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = description;
    [message setThumbImage:image];
    
    
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = urlStr;
    message.mediaObject = ext;
    
    
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;
    [WXApi sendReq:req];
}

- (void)sendLinkContentWithURlStr:(NSString *)urlStr
                         andTitle:(NSString *)title
                      andImageURL:(NSString *)imageURL
                       andDescription:(NSString *)description
                         andScene:(int)scene {
    
    UIImage * image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:imageURL];
    
    if(!image){
        [[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:imageURL] options:SDWebImageLowPriority|SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
      
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
        }];
    }else{
        [[WXClient sharedClient]sendLinkContentWithURlStr:urlStr
                                                 andTitle:title
                                           andDescription:description
                                                 andImage:[UIImage scaleToSize:image size:CGSizeMake(50, 50)]
                                                 andScene:scene];
    }
}

@end
